package com.example.assemblyutil.util;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.RelativeLayout;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * descrition:
 */
public class WatermarkUtils {
    /**
     * 添加水印
     * @param src
     * @param title
     * @param location
     * @return
     */
    public static Bitmap createBitmap(Bitmap src, String title, String location) {
        if (src == null) {
            return src;
        }
        int width = src.getWidth();
        int height = src.getHeight();

        Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas mCanvas = new Canvas(newBitmap);
        // 往位图中开始画入src原始图片
        mCanvas.drawBitmap(src, 0, 0, null);

//        if(null != waterMak){
//            int ww = waterMak.getWidth();
//            int wh = waterMak.getHeight();
//            // 在src的右下角添加水印
//            Paint paint = new Paint();
//            //paint.setAlpha(100);
//            mCanvas.drawBitmap(waterMak, width - ww - 5, height - wh - 5, paint);
//        }
        // 开始加入文字
        if (null != title) {
            Paint textPaint = new Paint();
            //设置字体颜色
            textPaint.setColor(Color.RED);
            //设置字体大小
            int i = (width + height) / 300 * 5;
            textPaint.setTextSize(i);
            //阴影设置
            textPaint.setShadowLayer(1f, 1, 1, Color.DKGRAY);
            String familyName = "宋体";
            Typeface typeface = Typeface.create(familyName,
                    Typeface.BOLD_ITALIC);
            textPaint.setTypeface(typeface);
            textPaint.setTextAlign(Paint.Align.CENTER);



            TextPaint textPaint2 = new TextPaint();
            //设置字体颜色
            textPaint2.setColor(Color.RED);
            //设置字体大小
            //根据图片大小设置字体大小
            textPaint2.setTextSize(i);
            //阴影设置
            textPaint2.setShadowLayer(1f, 1, 1, Color.DKGRAY);
            String familyName2 = "宋体";
            Typeface typeface2 = Typeface.create(familyName2,
                    Typeface.BOLD_ITALIC);
            textPaint2.setTypeface(typeface2);
            textPaint2.setTextAlign(Paint.Align.CENTER);
            float textWidth = textPaint2.measureText(title);
            //文字 添加位置
            mCanvas.drawText(title,  textWidth/2+50, 100, textPaint);          //时间
            StaticLayout layout = new StaticLayout(location, textPaint2, width/2, Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);

            Log.e("aaa","textWidth:"+textWidth+"width:"+width);
            mCanvas.translate(  width/4,  150);
            layout.draw(mCanvas);

        }
        mCanvas.save();
        //保存
        mCanvas.restore();
        return newBitmap;
    }

    /**
     * 保存
     * @param image
     */
    public static void saveImageToGallery2(Bitmap image, Context context) {
        long timeStamp = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(timeStamp));
        String fileName = sd + ".png";
        final ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES
                + File.separator + "erweima16"); //Environment.DIRECTORY_SCREENSHOTS:截图,图库中显示的文件夹名。"dh"
        values.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
        values.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
        values.put(MediaStore.MediaColumns.DATE_ADDED, timeStamp / 1000);
        values.put(MediaStore.MediaColumns.DATE_MODIFIED, timeStamp / 1000);
        values.put(MediaStore.MediaColumns.DATE_EXPIRES, (timeStamp + DateUtils.DAY_IN_MILLIS) / 1000);
        values.put(MediaStore.MediaColumns.IS_PENDING, 1);

        ContentResolver resolver = context.getContentResolver();
        final Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        try {
            // First, write the actual data for our screenshot
            try (OutputStream out = resolver.openOutputStream(uri)) {
                if (!image.compress(Bitmap.CompressFormat.PNG, 100, out)) {
                    throw new IOException("Failed to compress");
                }
            }
            // Everything went well above, publish it!
            values.clear();
            values.put(MediaStore.MediaColumns.IS_PENDING, 0);
            values.putNull(MediaStore.MediaColumns.DATE_EXPIRES);
            resolver.update(uri, values, null, null);
        } catch (IOException e) {
            resolver.delete(uri, null,null);
        }
    }

    public static Bitmap addWaterMark(Bitmap src, String water, Context context){
        Bitmap tarBitmap = src.copy(Bitmap.Config.ARGB_8888, true);
        int w = tarBitmap.getWidth();
        int h = tarBitmap.getHeight();
        Canvas canvas = new Canvas(tarBitmap);
        //启用抗锯齿和使用设备的文本字距
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        //字体的相关设置
        textPaint.setTextSize(35.0f);//字体大小
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        textPaint.setColor(Color.BLACK);
        textPaint.setShadowLayer(3f, 1, 1,context.getResources().getColor(android.R.color.background_dark));
        //图片上添加水印的位置，这里设置的是中下部3/4处
        canvas.drawText(water, (float)(w*0.05), (float) (h*0.9), textPaint);
//        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.save();
        canvas.restore();
        return tarBitmap;
    }
}
